﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibMSNP.Client;

namespace Beaup.UnitTests
{
    class MSNPClientStateMapTester : UnitTester
    {
        private MSNPClientStateMap _state;
        private string[] _states = new string[100000];
        public MSNPClientStateMapTester()
            : base()
        {
            _state = new MSNPClientStateMap();
            _errors = new LinkedList<UnitError>();
        }

        public override void RunTest()
        {
            int i = 0;
            int j = 0;
            int row = 0, column = 0;
            States savedCurrentState;
            int[,] validMoves = { {1, 1, 0, 0, 0},  //valid moves matrix!
                                  {1, 0, 1, 0, 0}, 
                                  {1, 0, 0, 1, 0},
                                  {1, 0, 0, 0, 1},
                                  {1, 0, 0, 0, 0}
                                };


            Random rand = new Random();
            while (i++ != 100000)
            {
                savedCurrentState = _state.CurrentState;
                row = (int)savedCurrentState;
                if (rand.Next() % 2 == 0)
                    column = (int)_state.NextState();
                else
                    column = (int)_state.PreviousState();
                if (validMoves[row, column] == 1)
                    _states[j++] = System.Enum.GetName(typeof(States), _state.CurrentState);
                else
                {
                    _passed = false;
                    _errors.AddLast(new UnitError(i, "Invalid Move from " + System.Enum.GetName(typeof(States), savedCurrentState) + " to " + System.Enum.GetName(typeof(States), _state.CurrentState)));
                }
            }
        }

        public override void PrintErrors()
        {
            if (!_passed)
            {
                foreach (UnitError item in _errors)
                {
                    Debug.PrintMessage(item.ToString());
                    Debug.PrintMessage("UnitTest Failed");
                }
            }
            else
            {
                Debug.PrintMessage("UnitTest Passed");
            }
        }

        public override void PrintResults()
        {
            for (int i = 0; i < _states.Length; i++)
                Debug.PrintMessage(i + ") " + _states[i]);
        }
    }
}
